package contents;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import database.Database;
import database.DatabaseEntities;

public class LoggingContent {
    public static boolean tryToLogIn(HttpServletRequest request) throws SQLException {
        boolean success = false;
        
        String clientIPAddress = (String) request.getRemoteAddr();
        String username = (String) request.getParameter("username");
        String passwordHash = (String) request.getParameter("password");
        
        HttpSession session = request.getSession();
        
        String userRole = Database.selectUserRoleOfUsernameAndPassword(username, passwordHash);
        session.setAttribute("userRole", userRole);
        
        if (userRole.equals("visitor")) {
            success = false;
            Database.logAccess(clientIPAddress, username, passwordHash, success);
        } else {
            success = true;
            Database.logAccess(clientIPAddress, username, passwordHash, success);
        }
    
        if (userRole.equals("admin")) {
            session.setAttribute("userName", "Administrátor");
               
        } else if (userRole.equals("student")) {
            Integer userId = Database.selectUserId(username, Database.UserRole.STUDENT);
            session.setAttribute("userId", userId.intValue());
            
            DatabaseEntities.Student student = Database.selectNameClassIdAndClassNameOfStudent(userId.intValue());
            session.setAttribute("userName", student.name);
            session.setAttribute("className", student.className);
            session.setAttribute("classId", student.classId);
            
        } else if (userRole.equals("teacher")) {
            Integer userId = Database.selectUserId(username, Database.UserRole.TEACHER);
            session.setAttribute("userId", userId.intValue());
            
            DatabaseEntities.Teacher teacher = Database.selectNameOfTeacher(userId.intValue());
            session.setAttribute("userName", teacher.name);

        } else if (userRole.equals("parent")) {
            Integer userId = Database.selectUserId(username, Database.UserRole.PARENT);
            session.setAttribute("userId", userId.intValue());
            
            DatabaseEntities.Parent parent = Database.selectNameStudentNameClassIdAndClassNameOfParent(userId.intValue());
            session.setAttribute("userName", parent.name);
            session.setAttribute("childName", parent.studentName);
            session.setAttribute("className", parent.className);
            session.setAttribute("classId", parent.classId);
            
        }
        
        return success;
    }
    
    public static boolean changePassword(HttpServletRequest request) throws SQLException {
        String oldPasswordHash = (String) request.getParameter("oldPassword");
        String newPasswordHash = (String) request.getParameter("newPassword");
        
        HttpSession session = request.getSession();
        String userRole = (String) session.getAttribute("userRole");
        
        String username;
        if (userRole.equals("student")) {
            username = Database.selectUsernameOfUser(Database.UserRole.STUDENT, ((Integer)session.getAttribute("userId")).intValue());            
        } else if (userRole.equals("parent")) {
            username = Database.selectUsernameOfUser(Database.UserRole.PARENT, ((Integer)session.getAttribute("userId")).intValue());
        } else if (userRole.equals("teacher")) {
            username = Database.selectUsernameOfUser(Database.UserRole.TEACHER, ((Integer)session.getAttribute("userId")).intValue());
        } else {
            username = Database.selectUsernameOfUser(Database.UserRole.ADMIN, -1);
        }
        return Database.updateUserPassword(username, oldPasswordHash, newPasswordHash);
    }
    
    private LoggingContent() {
    }
}
